library(scales)
# Variables (Favorability toward Political Parties and Leaders)
vars <- c("likeCon","likeLab","likeLD", "likeSNP", "likeBrexitParty",
          "likeJohnson","likeStarmer","likeDavey","likeSturgeon","likeTice")

# Function for Calculating Pre-Post 7-day Averages and Differences
analyze_var_table <- function(var) {
  sub_data <- bes_w22 |>
    mutate(
      pre_post = factor(ifelse(treatment == 1, "Post", "Pre"),
                        levels = c("Pre","Post")),
      pid = as_factor(partyId),
      y = .data[[var]]
    ) |>
    filter(
      y <= 10,
      partyId %in% c(1, 2, 10),
      date_diff >= -7, date_diff <= 6 #7-day bandwidth
    )
  
  means <- sub_data |>
    group_by(pid, pre_post) |>
    summarise(m = mean(y, na.rm = TRUE), .groups = "drop") |>
    pivot_wider(names_from = pre_post, values_from = m)
  
  tests <- sub_data |>
    group_by(pid) |>
    summarise(
      ttest = list(t.test(y ~ pre_post)),
      .groups = "drop"
    ) |>
    mutate(
      diff = map_dbl(ttest, ~ .x$estimate[2] - .x$estimate[1]) # Post - Pre
      ) |>
    select(-ttest)
  

  out <- left_join(means, tests, by = "pid") |>
    mutate(variable = var) |>
    select(variable, pid, Pre, Post, diff)
  
  return(out)
}

# Table H1 and H2
results_table <- map_dfr(vars, analyze_var_table)
print(results_table,n=30)


#Distribution of Conservative FT among Labour supporters before and after the video
plotdf_likecon <- bes_w22 |>
  filter(partyId == 2, likeCon <= 10, date_diff >= -7, date_diff <= 6) |>
  mutate(bin = cut(likeCon, breaks = seq(-0.5, 10.5, by = 1),
                   labels = 0:10, right = TRUE)) |>
  count(treatment, bin, .drop = FALSE) |>
  group_by(treatment) |>
  mutate(prop = n / sum(n)) |>
  ungroup() |>
  complete(treatment, bin, fill = list(n = 0, prop = 0)) |> 
  mutate(pre_post=ifelse(treatment==0,"Pre","Post") |> factor(levels = c("Pre","Post")))

#Figure H1
gg_likecon <- ggplot(plotdf_likecon, aes(x = bin, y = prop, fill = pre_post)) +
  geom_col(position = position_dodge(width = 0.9), width = 0.9, linewidth = 0.5) +
  scale_y_continuous(labels = percent_format()) +
  scale_fill_discrete(name = "Video Release")+
  labs(x = "Conservative Favorability", y = "")+
  theme(text = element_text(size=36))
gg_likecon


plotdf_likejohnson <- bes_w22 |>
  filter(partyId == 2, likeJohnson <= 10, date_diff >= -7, date_diff <= 6) |>
  mutate(bin = cut(likeJohnson, breaks = seq(-0.5, 10.5, by = 1),
                   labels = 0:10, right = TRUE)) |>
  count(treatment, bin, .drop = FALSE) |>
  group_by(treatment) |>
  mutate(prop = n / sum(n)) |>
  ungroup() |>
  complete(treatment, bin, fill = list(n = 0, prop = 0)) |> 
  mutate(pre_post=ifelse(treatment==0,"Pre","Post") |> factor(levels = c("Pre","Post")))

#Figure H2
gg_likejohnson <- ggplot(plotdf_likejohnson, aes(x = bin, y = prop, fill = pre_post)) +
  geom_col(position = position_dodge(width = 0.9), width = 0.9, linewidth = 0.5) +
  scale_y_continuous(labels = percent_format()) +
  scale_fill_discrete(name = "Video Release")+
  labs(x = "Conservative Favorability", y = "")+
  theme(text = element_text(size=36))
gg_likejohnson
